
//  DeterenceRootViewController.m
//  Yihubang
//
//  Created by xmy on 2016/11/24.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "DeterenceRootViewController.h"
#import "DeterrenceVC.h"
#import "CallHelpViewController.h"
@interface DeterenceRootViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource,openHelpDelegate,BeginGuideDelegate>

@property (nonatomic ,strong) UIPageViewController *mPageViewController;
@property (nonatomic ,strong) NSArray *viewControllerArr;
@property (nonatomic ,strong) UIButton *Left_menu;
@end

@implementation DeterenceRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setCenterButton];
    [self setTitleView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setCenterButton{
    self.Left_menu = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.Left_menu setImage:[UIImage imageNamed:@"个人中心"]  forState:UIControlStateNormal];
    [self.Left_menu setFrame:CGRectMake(0, 0, 35, 44)];
    [self.Left_menu addTarget:self action:@selector(showUserCenter:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*userHeadItem = [[UIBarButtonItem alloc] initWithCustomView:self.Left_menu];
    [self.navigationItem setLeftBarButtonItem:userHeadItem animated:YES];
}
- (void) setTitleView{
    UIStoryboard *HelpStory = [UIStoryboard storyboardWithName:@"GuardMe" bundle:[NSBundle mainBundle]];
    CallHelpViewController * CallHelpViewController = [HelpStory instantiateViewControllerWithIdentifier:@"CallHelpViewController"];
    CallHelpViewController.delegate =self;
    DeterrenceVC * DeterrenceVC = [HelpStory instantiateViewControllerWithIdentifier:@"DeterrenceVC"];
    DeterrenceVC.delegate = self;
    self.viewControllerArr =@[DeterrenceVC,CallHelpViewController];
    self.mPageViewController =self.childViewControllers.firstObject;
    self.mPageViewController.delegate=self;
    self.mPageViewController.dataSource=self;
    [self.mPageViewController setViewControllers:self.userClient.openTypeId ? @[CallHelpViewController]:@[DeterrenceVC]
                                       direction:UIPageViewControllerNavigationDirectionReverse
                                        animated:YES
                                      completion:nil];
}
-(NSArray *)viewControllerArr{
    if (!_viewControllerArr) {
        _viewControllerArr = [NSMutableArray array];
    }
    return _viewControllerArr;
}
#pragma mark - UIPageViewControllerDataSource And UIPageViewControllerDelegate
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    NSInteger index = [self.viewControllerArr indexOfObject:viewController];
    if (index==0 || index == NSNotFound) {
        return nil;
    }else{
        return self.viewControllerArr[--index];
    }
}
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSInteger index = [self.viewControllerArr indexOfObject:viewController];
    if (index == self.viewControllerArr.count-1) {
        return nil;
    }
    else{
        return self.viewControllerArr[++index];
    }
}
-(void)removeDataSource{
    for (id testView in self.mPageViewController.view.subviews) {
        UIScrollView *scrollView = (UIScrollView *)testView;
        if ([scrollView isKindOfClass:[UIScrollView class]]) {
            scrollView.scrollEnabled = NO;
            // scrollView.bounces = self.shouldBounce;
        }
    }
}
-(void)resetDataSource{
    for (id testView in self.mPageViewController.view.subviews) {
        UIScrollView *scrollView = (UIScrollView *)testView;
        if ([scrollView isKindOfClass:[UIScrollView class]]) {
            scrollView.scrollEnabled = YES;
            // scrollView.bounces = self.shouldBounce;
        }
    }
}

-(void)removeDeterrenceGuideViewActionAfter{
    [self.mPageViewController setViewControllers:@[self.viewControllerArr [1]]
                                       direction:UIPageViewControllerNavigationDirectionForward
                                        animated:YES
                                      completion:nil];
}

-(void)callHelpViewjumpAction{
    @weakify(self)
    [self.mPageViewController setViewControllers:@[self.viewControllerArr [0]]
                                       direction:UIPageViewControllerNavigationDirectionReverse
                                        animated:YES
                                      completion:^(BOOL finished){
                                          @strongify(self)
                                          [self showOnceMyCenter];
                                      }];
}
-(void)openHelpSuccessful:(BOOL)success{
    if(success){
        [self removeDataSource];
    }else{
        [self resetDataSource];
    }
    [self.Left_menu setEnabled:!success];
    [[KGlobalView sharedKGlobalView] setHidden:success];
}

- (void)showOnceMyCenter{
    [self showUserCenter:nil];
}

-(void)deterrenceVCBeginGuideAction{
    [self removeDeterrenceGuideViewActionAfter];
}

-(void)CallHelpVCBeginGuideAction{
    [self callHelpViewjumpAction];
}

@end
